<?php
class keu_ledger_model extends MY_Model {
	var $model = 'keu_ledger';
	
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	gl.*, ofc.nama_kantor, 
							act.nama_cost_center, 
							coa.nama_coa, NVL(gl.begin_db,0)-NVL(gl.begin_cr,0) saldo_awal
					FROM	keu_ledger gl, 
							sw_kantor ofc, sw_cost_center act, keu_coa coa
					WHERE	
							ofc.kd_kantor = gl.kd_kantor  and
							act.id_cost_center_pk = gl.kd_cost_center AND
							coa.id_coa = gl.id_coa and
							
							(gl.tahun LIKE '%".post('keyword')."%'
							OR UPPER(ofc.nama_kantor) LIKE UPPER('%".post('keyword')."%')
							OR UPPER(act.nama_cost_center) LIKE UPPER('%".post('keyword')."%')
							OR UPPER(gl.id_coa) LIKE UPPER('%".post('keyword')."%')							
							OR UPPER(coa.nama_coa) LIKE UPPER('%".post('keyword')."%')							
							OR NVL(gl.begin_db,0)-NVL(gl.begin_cr,0) LIKE '%".post('keyword')."%'	
							)
					ORDER BY ".(post('sort')=='' ? 'tahun' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
	
	
	function Add($arr_data){
		$this->db->insert('keu_ledger', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('keu_ledger', $arr_data, $arr_rule);
	}
	
	function GetSingle($tahun, $kd_kantor, $kd_cost_center,  $id_coa){
		$result = $this->db->get_where('keu_ledger', array('tahun' => $tahun,'kd_kantor' => $kd_kantor,'kd_cost_center' => $kd_cost_center,'id_coa' => $id_coa));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($tahun, $kd_kantor, $kd_cost_center,  $id_coa)
	{
		$ls_sql	= "	SELECT	gl.*,  keu_coa.nama_coa,
							case when NVL(db1,0)+NVL(db2,0)+NVL(db3,0)+NVL(db4,0)+NVL(db5,0)+NVL(db6,0)+NVL(db7,0)+NVL(db8,0)+NVL(db9,0)+NVL(db10,0)+NVL(db11,0)+NVL(db12,0)+
							NVL(cr1,0)+NVL(cr2,0)+NVL(cr3,0)+NVL(cr4,0)+NVL(cr5,0)+NVL(cr6,0)+NVL(cr7,0)+NVL(cr8,0)+NVL(cr9,0)+NVL(cr10,0)+NVL(cr11,0)+NVL(cr12,0) > 0
							then 0 else 1 end edit,
							case when (select count(*) from keu_ledger where 
							kd_kantor  = gl.kd_kantor and kd_cost_center =gl.kd_cost_center 
							 ) > 1
							then 0 else 1 end edit2
					FROM	keu_ledger gl, keu_coa
					WHERE	gl.tahun = '".$tahun."' and
							gl.kd_kantor = '".$kd_kantor."' and
							gl.kd_cost_center = '".$kd_cost_center."' and
							gl.id_coa = '".$id_coa."'  and
							keu_coa.id_coa = gl.id_coa
							";
							
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	
	function DeleteSingle($as_code)
	{
		try {
			$this->db->delete('keu_ledger', array('doc_id' => $as_code));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
	

}
?>